<?php

namespace app\admin\controller;

use Couchbase\UserSettings;
use think\Db;
use think\Request;

class Con1 extends AdminController
{
    public function index()
    {
        $list=Db::table('cc_admin_user')->field(['au_id', 'au_name','au_tel'])->order('au_id', 'ASC')->select();
//        dump($list);die;
        foreach ($list as $v) {
            $r_ids = Db::table('cc_user_role')->field('r_id')->where('au_id', $v['au_id'])->select();
//            dump($r_ids);die;
            //定义空数组
            $roles = array();
            //遍历
            foreach ($r_ids as $value) {
                $roles[] = Db::table('cc_admin_role')->where('r_id', $value['r_id'])->field('r_name')->find();
            }
            $v['role'] = $roles; //将新得到角色信息放置到$v中
//            dump($roles);
            $arr[] = $v;
//            dump($v);die;
        }
        return view('admin@con1/tables',['list' => $arr]);
    }

    public function add()
    {
        return view('admin@con1/add');
    }

    public function doAdd(Request $request)
    {
        $p = input('post.'); // 获取POST数据
//        dump($p);
        $data = [
           'au_name'     => $p['name'],
           'au_password'     => md5($p['pass']),
           'au_tel'      => $p['tel']
      ];
       $result = Db::table('cc_admin_user')->data($data)->insert();
       $lastid = Db::table('cc_admin_user')->getLastInsID();
       $mr = [
           'au_id' => $lastid,
           'r_id' => 3
       ];
       $res = Db::table('cc_user_role')->data($mr)->insert();

        if ($res > 0) {
            return $this->success('添加成功!', url('admin/con1/index'));
        } else {
            return $this->error('添加失败!');
        }

    }


    public function edit($id)
    {
        $row = Db::table('cc_admin_user')->find($id);
//        dump($row);die;
        return view('admin@con1/edit',['row'=>$row]);
    }

    public function doEdit(Request $request,$id)
    {
        $info = input('get.');

//        dump($info);die;

        // 手动处理数据
        $data = [
            'au_name'     => $info['au_name'],
            'au_tel'      => $info['au_tel']
        ];

        $result = Db::table('cc_admin_user')->where('au_id', $id)->update($data);

        if ($result > 0) {
            return $this->success('更新成功!', url('admin/con1/index'));
        } else {
            return $this->error('更新失败!');
        }
    }

    public function role($id)
    {
        $user = Db::table('cc_admin_user')->where('au_id',$id)->find();
//        dump($role);die;
        $role = Db::table('cc_admin_role')->select();
//        dump($node);die;
        return view('admin@con1/role',['user'=>$user,'role'=>$role]);
    }

    public function rolelist($id)
    {
        // 查询角色信息
        $list = Db::table('cc_admin_role')->select();
        // 查询当前用户信息
        $user = Db::table('cc_admin_user')->where('au_id',$id)->find();
        // 获取当前用户角色信息
        $rolelist = Db::table('cc_user_role')->where('au_id',$id)->select();
//        dump($rolelist);die;
        // 定义空数组
        $myrole = [];

        // 重组
        foreach($rolelist as $v) {

//                dump($v);die;
//
                $myrole[]=Db::table('cc_admin_role')->where('r_id',$v['r_id'])->select();


        }
        dump($myrole);die;
//        dump($users);die;

        return view('admin@Con1/rolelist',['list'=>$list,
            'user'=>$user,
            'myrole'=>$myrole
            ]);

    }

    public function doRole(Request $request,$id)
    {
        $info = $request->put();
//        dump($id);die;
//        dump($info);die;

        // 判断是否选择了权限
        if (!empty($info['rid'])){
            // 删除 管理员角色表中 所有 此ID下得数据
            Db::table('cc_user_role')->where('au_id',$id)->delete();
            // 获取已选择得角色
            $data = $info['rid'];
//            dump($data);die;
            // 获取 权限 ID
            $au_id = $info['au_id'];


            foreach($data as $v){

//                dump($data);die;
                $result = Db::table('cc_user_role')->insert(['au_id'=>$au_id,'r_id'=>$v]);
            }
            if ($result > 0) {
                return $this->success('权限更新成功!', url('admin/Con1/index'));
            } else {
                return $this->error('权限更新失败!');
            }
        }else{
            return $this->error ('未选择角色,修改失败!');
        }
    }


}